package com.migo.api;

import com.migo.annotation.IgnoreAuth;
import com.migo.entity.SysOssEntity;
import com.migo.oss.OSSFactory;
import com.migo.service.SysOssService;
import com.migo.utils.R;
import com.migo.utils.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * Created by Wes on 2018/11/19.
 */
@RestController
@RequestMapping("/api")
public class ApiCommonController {


    @Autowired
    OSSFactory ossFactory;

    @Autowired
    private SysOssService sysOssService;
    /**
     * 上传文件
     */
    @IgnoreAuth
    @RequestMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file) throws Exception {
        String fileName = "";
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        fileName = file.getOriginalFilename();
        String url = ossFactory.build().upload(fileName,file.getBytes());
        SysOssEntity ossEntity = new SysOssEntity();
        ossEntity.setUrl(url);
        ossEntity.setCreateDate(new Date());
        sysOssService.save(ossEntity);
        return R.ok().put("data", ossEntity);
    }


}
